import * as constants from './constants'
import axios from "axios"
import { generateUrl } from "../../../../util/generateUrl"
import { fromJS, } from "immutable";
import { configUrl } from "../../../../config/config"
export const changeShowState = ( index, name ) => {
  return {
    type: constants.CHANGE_SHOW_STATE,
    data: {
      index,name
    }
  }
}
const changeCurrentPage = (flag,currentPage) =>{
  return {
     type: constants.CHANGE_CURRENT_PAGE,
    data: flag+currentPage
  }
}
export const getNextPage = (flag,currentPage,First,Second,Third,keyword,pageSize) =>{
  return (dispatch) =>{
    let url = generateUrl(First,Second,Third,keyword,currentPage+flag,pageSize)
    axios.get(url).then(res=>{
      dispatch(changePageContent(res.data.data))
      dispatch(changeCurrentPage(flag,currentPage))
    }).catch(err=> {
      alert("发生错误:"+err)
    })
  }
}
export const changeStartPage = (page) => {
  return {
    type: constants.CHANGE_START_PAGE,
    data: page
  }
}
const showSearchKeyword = (keyword) =>{
  return {
    type: constants.SHOW_SEARCH_KEYWORD,
    data: keyword
  }
}
export const getTopKKeyword = (keyword) =>{
  return (dispatch) =>{
    axios.get(`${configUrl.getKeyWord}?keyword=${keyword}`,).then(res=>{
        dispatch(showSearchKeyword(res.data.data))
      }).catch(e=>{
        dispatch(showSearchKeyword([{key:"无",num:1}]))
    })
  }
}
export const setInputValue = (value) =>{
  return {
    type: constants.CHANGE_INPUT_VALUE,
    data: value
  }
}


const changeConditionSate = (index,name)=>{
  return {
    type: constants.CHANGE_CONDITION_STATE,
    data:{
      index,name
    }
  }
}
export const getDiffConditionContent = (index,name,First,Second,Third,keyword,pageSize) =>{
  return (dispatch) =>{
    let first = First.toJS();
    let second = Second.toJS();
    let third = Third.toJS();
    if(name=='FirstFilter'){
      for(let i = 0; i < first.length;i++){
        if(first[i].id===index){
          first[i].condition = 1;
          continue;
        }
        first[i].condition = 0;
      }
    }else if(name=='SecondFilter'){
      for(let i = 0; i < second.length;i++){
        if(second[i].id===index){
          second[i].condition = 1;
          continue;
        }
        second[i].condition = 0;
      }
    }else{
      for(let i = 0; i < third.length;i++){
        if(third[i].id===index){
          third[i].condition = 1;
          continue;
        }
        third[i].condition = 0;
      }
    }
    let url  = generateUrl(fromJS(first),fromJS(second),fromJS(third),keyword,1,pageSize)
    axios.get(url).then(res=>{
      dispatch(changeSearchContent(res.data.data))
      dispatch(changeConditionSate(index,name))
    }).catch(err=>{
      alert("发生错误: "+err)
    })
  }
}
export const getSearchContent = (First,Second,Third,keyword,pageNum,pageSize) =>{
  return (dispatch) =>{
    let url  = generateUrl(First,Second,Third,keyword,pageNum,pageSize)
    axios.get(url).then(res=>{
      dispatch(changeSearchContent(res.data.data))
    }).catch(err=> {
        alert("发生错误:"+err)
      })
  }
}

const changeSearchContent = ( data ) =>{
  return {
    type: constants.ChANGE_SEARCH_CONTENT,
    data: data
  }
}

const changePageContent =(data) =>{
  return {
    type : constants.CHANGE_PAGE_CONTENT,
    data: data
  }
}
export const addClickNum = (id) =>{
  return (dispatch) => {
    axios.get(`${configUrl.addClickNum}?id=${id}`).then(res=>{
      const w = window.open(res.data.data)
      w.location.href(res.data.data)
    }).catch(err=>{
      console.log("发生错误")
    })
  }
}

export const getAnswer = (question) =>{
  return (dispatch) =>{
    axios.get(`${configUrl.getFromTestKg}?sentence=${question}`).then(res=>{
      dispatch(setAnswer(res.data))
    }).catch(err=>{
      console.log(err)
    })
  }
}
const setAnswer = (data) =>{
  return {
    type: constants.SET_SHOW_ANSWER,
    data: data
  }
}
